

#import "HomeViewController.h"
#import "HomeHeader.h"
#import "HomeCell.h"
#import "MineViewController.h"
#import "HomeFlowLayout.h"
#import "EditViewController.h"
#import "SignViewController.h"
#import "FavoriteViewController.h"

@interface HomeViewController ()
@property (nonatomic, strong) HomeHeader *header;
@property (nonatomic, strong) NSArray <SignModel *>*array;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [self.view sendSubviewToBack:self.collectionView];
    [self setupNaviBarColor:[UIColor clearColor] titleColor:[UIColor blackColor]];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (void)initUI{
    HomeFlowLayout *layout  = [[HomeFlowLayout alloc] init];
    CGFloat w = (SCREEN_WIDTH - 20 * 3) / 2;
    CGFloat h = 229.0 / 153 * w;
    layout.itemSize = CGSizeMake(w,h);
    layout.sectionInset = UIEdgeInsetsMake(20, 20, -20, 20);
    layout.minimumLineSpacing = 20;
    layout.minimumInteritemSpacing = 20;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 307);
    self.collectionView.collectionViewLayout = layout;
    [self.collectionView registerNib:[HomeCell nib] forCellWithReuseIdentifier:[HomeCell ID]];
    [self.collectionView registerNib:[HomeHeader nib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[HomeHeader ID]];
    NSLayoutConstraint *top = [self.collectionView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeTop c:0];
    NSLayoutConstraint *left = [self.collectionView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:0];
    NSLayoutConstraint *right = [self.collectionView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:0];
    NSLayoutConstraint *bottom = [self.collectionView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeBottom c:0];
    [self.view addConstraints:@[top,left,right,bottom]];
}
- (void)backBtnAction{
    [(MineViewController *)APP.meWindow.rootViewController show];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    EditViewController *vc = [[EditViewController alloc] init];
    vc.isedit = NO;
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(APP.user.account)];
    vc.model.ID = [NSDate currentTimeStamp:@"yyyy/MM/dd HH:mm:ss"];
    vc.model.account = APP.user.account;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)loadData{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(APP.user.account)];
   [SignModel bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.array = array;
            [self.collectionView reloadData];
        });
    }];;
}
- (void)addNotify{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:notifySignUpdate object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:notifyNoteUpdate object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:notifyUserUpdate object:nil];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCell ID] forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];
    WS(self)
    cell.block = ^{
        SignViewController *vc = [SignViewController new];
        vc.title = LS(@"标签详情");
        vc.model = weakself.array[indexPath.row];
        [weakself.navigationController pushViewController:vc animated:YES];
    };
    return  cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    self.header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[HomeHeader ID] forIndexPath:indexPath];
    WS(self)
    self.header.block = ^(NSString * _Nonnull text) {
        if ([text isEqualToString:@"标签"]) {
            SignViewController *vc = [SignViewController new];
            vc.title = LS(@"添加新标签");
            [weakself.navigationController pushViewController:vc animated:YES];
        }else if ([text isEqualToString:@"便签"]){}
    };
    return  self.header;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FavoriteViewController *vc = [FavoriteViewController new];
    vc.title = LS(@"便签");
    vc.sign = self.array[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    CGFloat w = (SCREEN_WIDTH - 20 * 3) / 2;
//    CGFloat h = 229.0 / 153 * w;
//    return  CGSizeMake(w,h);
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return  UIEdgeInsetsMake(-60, 20, 20, 20);
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(SCREEN_WIDTH, 307);
//    
//}


@end
